#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define MAX_SIZE 1024

int main(void)
{
    int fd;
    char buf[MAX_SIZE]; //缓冲区
    char get[MAX_SIZE]; //要写入的信息

    char dir[50] = "/dev/myDevice";

    fd = open(dir, O_RDWR | O_NONBLOCK);

    if (fd != -1)
    {
        while(1) {
            //读信息
            read(fd, buf, sizeof(buf));
            printf("%s\n", buf);

            //写信息
            printf("input (q to quit):");
            scanf(" %s", get);
            if (strcmp(get, "q") == 0) {
                break;
            }
            write(fd, get, strlen(get));

            //读刚才写的信息
            read(fd, buf, strlen(buf));
            printf("device Message: %s\n", buf);

        }
        close(fd);
    }
    else
    {
        printf("Device open failed\n");
        return -1;
    }

    return 0;
}
